package system;
import java.sql.*;
public class sqlOperation{
	
	public int getCount(String queryCondition) {
		int count=0;
		Connection con;
	    Statement sql;
	    ResultSet rs=null;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
	    try{
			String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
			String user = "sa";
			String password = "sa";
			con=DriverManager.getConnection(uri,user,password);
			sql=con.createStatement();
			rs=sql.executeQuery(queryCondition);
			if(rs.next()){
				count=rs.getInt(1);			
			}
			con.close();
		}
		catch(SQLException e1){}
	    return count;
	}
	
	public String[] getQuerySet(String queryCondition,int count) {	
		Connection con;
	    Statement sql;
	    ResultSet rs=null;
	    String[] result=new String[count];
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		rs=sql.executeQuery(queryCondition);
		int i=0;
		while(rs.next()) {
			result[i]=(rs.getString(1)).trim();
			i++;
		}
		con.close();
	    } catch(SQLException e1){}
		return result;
	}
	
	public String[] getQueryLine(String queryCondition,int count) {	
		Connection con;
	    Statement sql;
	    ResultSet rs=null;
	    String[] result=new String[count];
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		rs=sql.executeQuery(queryCondition);
		rs.next();
		for(int i=1;i<=count;i++) {
			result[i-1]=((String)(rs.getString(i))).trim();
		}
		con.close();
	    } catch(SQLException e1){}
		return result;
	}
	
	public int getInsert(String insertCondition) {
		Connection con;
	    Statement sql;
	    int m=0;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		m=sql.executeUpdate(insertCondition);
		con.close();
	    } catch(SQLException e1){}
        
        return m;
	}
	
	public int getDelete(String deleteCondition) {
		Connection con;
	    Statement sql;
	    int m=0;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		m=sql.executeUpdate(deleteCondition);
		con.close();
	    } catch(SQLException e1){}
        
        return m;
	}
	
	public int getUpdate(String updateCondition) {
		Connection con;
	    Statement sql;
	    int m=0;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		m=sql.executeUpdate(updateCondition);
		con.close();
	    } catch(SQLException e1){}
        
        return m;
	}
	
	
	
}